package com.zgjkhis.modules.system.rest;

import com.zgjkhis.domain.PageResult;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.DrugWareHousingDetail;
import com.zgjkhis.modules.system.domain.vo.DrugRemainDetailUpdate;
import com.zgjkhis.modules.system.service.DrugWareHousingDetailService;
import com.zgjkhis.modules.system.service.dto.DrugRemainDto;
import com.zgjkhis.modules.system.service.dto.DrugRemainQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "库房管理：有效期提醒")
@RestController
@RequestMapping("/drugremain")
public class DrugReminderController {

    private final DrugWareHousingDetailService drugWareHousingDetailService;

    public DrugReminderController(DrugWareHousingDetailService drugWareHousingDetailService) {
        this.drugWareHousingDetailService = drugWareHousingDetailService;
    }


    @Log("分页查询效期提醒")
    @ApiOperation("分页查询效期提醒")
    @GetMapping(value = "/getList")
//    @PreAuthorize("@el.check('drugremain:list')")
    public ResponseEntity<PageResult<DrugRemainDto>> getList(DrugRemainQueryCriteria criteria, Pageable pageable){
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(drugWareHousingDetailService.findRemainByPage(criteria, pageable),HttpStatus.OK);
    }

    @Log("更新效期提醒")
    @ApiOperation("更新效期提醒")
    @PostMapping(value = "/update")
//    @PreAuthorize("@el.check('drugremain:list')")
    public ResponseEntity<Object> getList(@RequestBody DrugRemainDetailUpdate resource){
        DrugWareHousingDetail detail=new DrugWareHousingDetail();
        detail.setId(resource.getId());
        detail.setBatchNo(resource.getBatchNo());
        detail.setTermOfValidity(resource.getTermOfValidity());
        return new ResponseEntity<>(drugWareHousingDetailService.updateById(detail),HttpStatus.OK);
    }

    @Log("导出效期药品")
    @ApiOperation("导出效期药品")
    @GetMapping(value = "/downloadDrugByTerm")
//    @PreAuthorize("@el.check('drug:list')")
    public void downloadDrugByType(HttpServletResponse response, DrugRemainQueryCriteria criteria) throws IOException {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        List<DrugRemainDto> list=drugWareHousingDetailService.findRemainList(criteria);
        drugWareHousingDetailService.dowonload(list,response);
    }
}
